package cn.jtfadmin.base.lang.common.domain.entity;

import cn.jtfadmin.base.lang.common.security.UserDetails;
import cn.jtfadmin.base.lang.config.user.UserHolder;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 创建者实体监听器过程监听
 * @version 0.0.2
 * @since 0.0.2
 * @author jtf
 * @see EntityListener
 * @see CreateUserInfoEntity
 */
@Component
public class CreateUserInfoEntityListener implements EntityListener {



    /**
     * 设置id, 创建人id,创建时间,设置更新人,更新时间
     *
     * @param object 实体 必须是 CUDateUserEntity
     * @see CreateUserInfoEntity
     */
    public void prePersist(Object object) {
        if (object instanceof CreateUserInfoEntity) {
            CreateUserInfoEntity baseEntity = (CreateUserInfoEntity) object;
            if(Objects.isNull(baseEntity.getCreateUserId())){
                baseEntity.setCreateUserId(UserHolder.getUserId());
            }
            UserDetails userDetails = UserHolder.getUserDetails();
            if(Objects.nonNull(userDetails)){
                if(Objects.isNull(baseEntity.getCreateUsername())){
                    baseEntity.setCreateUsername(userDetails.getUsername());
                }
                if(Objects.isNull(baseEntity.getCreateUserRealname())){
                    baseEntity.setCreateUserRealname(userDetails.getDisplayName());
                }

            }
            preUpdate(object);
        }
    }





}
